package com.guofen.controller.sjq;

import com.guofen.pojo.SjqWaterProductionInspectionEquipment;
import com.guofen.service.sjq.SjqWaterProductionInspectionEquipmentService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/sjq/SjqWaterProductionInspectionEquipment")
@Controller
@Api(tags = "SjqWaterProductionInspectionEquipmentController", description = "新！——水处理生产现场记录表（设备检查)")
public class SjqWaterProductionInspectionEquipmentController {
    @Autowired
    private SjqWaterProductionInspectionEquipmentService sjqWaterProductionInspectionEquipmentService;

    @ApiOperation("添加记录_记录人")
    @RequestMapping(value = "/addOperator", method = RequestMethod.POST)
    @ResponseBody
    public Result addOperator(@ApiParam(name = "equipment_name", value = "设备名称", required = true) @RequestParam String equipment_name,
                          @ApiParam(name = "is_normal", value = "是否正常", required = true) @RequestParam String is_normal,
                          @ApiParam(name = "operator", value = "操作人员", required = true) @RequestParam String operator,
                          @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks){

        if (sjqWaterProductionInspectionEquipmentService.addForm(equipment_name, is_normal, operator, null, remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("查询最新记录")
    @RequestMapping(value = "/getLast", method = RequestMethod.GET)
    @ResponseBody
    public Result getLast(){
        if (sjqWaterProductionInspectionEquipmentService.findForm() != null)
            return ResultGenerator.genSuccessResult(sjqWaterProductionInspectionEquipmentService.findForm());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }

    @ApiOperation("添加记录_检查人")
    @RequestMapping(value = "/addInspector", method = RequestMethod.POST)
    @ResponseBody
    public Result addInspector(@ApiParam(name = "id", value = "id", required = true) @RequestParam String id,
                          @ApiParam(name = "inspector", value = "检查人员", required = true) @RequestParam String inspector,
                          @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks){

        int ID = Integer.parseInt(id);
        if (sjqWaterProductionInspectionEquipmentService.updateForm(ID, inspector, remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqWaterProductionInspectionEquipmentService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="time",value="时间",required=true) @RequestParam String time,
                         @ApiParam(name = "equipment_name", value = "设备名称", required = true) @RequestParam String equipment_name,
                         @ApiParam(name = "is_normal", value = "是否正常", required = true) @RequestParam String is_normal,
                         @ApiParam(name = "operator", value = "操作人员", required = true) @RequestParam String operator,
                         @ApiParam(name = "inspector", value = "检查人员", required = true) @RequestParam String inspector,
                         @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks) {
        SjqWaterProductionInspectionEquipment record = new SjqWaterProductionInspectionEquipment();
        record.setId(id);
        record.setTime(time);
        record.setEquipmentName(equipment_name);
        record.setIsNormal(is_normal);
        record.setOperator(operator);
        record.setInspector(inspector);
        record.setRemarks(remarks);
        return ResultGenerator.genSuccessResult(sjqWaterProductionInspectionEquipmentService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqWaterProductionInspectionEquipmentService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqWaterProductionInspectionEquipmentService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
